<?php


namespace app\common\controller;

use app\common\model\User;
use app\common\validate\Vld_User;
use think\exception\ValidateException;

class UserValidate
{
    public function checklogin($data) //检验登录
    {
        //1.创建验证器
        try
        {
            $validateres = validate(Vld_User::class)
                ->sceneLogin()
                ->check($data);
            if( $validateres)
            {//通过验证
                $myUser=new User();
                $result=$myUser->where($data)->find();
                if($result){
                    //判断用户是否可用
                    if ($result['is_active']!='是'){
                        return '此账户被禁用!';
                    }
                    //保存用户登录状态
                    //"data": {
                    //        "id": 500,
                    //        "rid": 0,
                    //        "username": "admin",
                    //        "mobile": "123",
                    //        "email": "123@qq.com",
                    //        "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1MTI1NDQyOTksImV4cCI6MTUxMjYzMDY5OX0.eGrsrvwHm-tPsO9r_pxHIQ5i5L1kX9RX444uwnRGaIM"
                    //    },
                    //    "meta": {
                    //        "msg": "登录成功",
                    //        "status": 200
                    //    }
                    $SessionData=[
                        'id'=>$result['user_id'],
                        'rid'=>0,
                        'username'=>$result['username'],
                        'email'=>$result['user_email'],
                        'mobile'=>$result['user_tel'],
                        'token'=>md5(time())
                    ];
                    session('user',$SessionData);
                    //1表示有这个用户
                    return 1;
                }else{
                    return '用户名或密码错误';
                }
            }
        }
        catch (ValidateException  $e) {
            // 验证失败 输出错误信息
            return $e->getMessage();
            //dump($e->getError());
        }
        // $myvalidate=new \app\common\validate\Validtae_admin();

    }
}
